// 根据积分返回等级的函数，1.5亿
export function getRank(points) {
    if (points < 100 || !points) return 1; // 小于100分，等级1
    const a = 15.537032635185835; // 常数a
    const b = 3.5; // 幂指数
    const effective = Math.pow(points / a, 1 / b);
    const rank = Math.floor(effective) + 1;
    if (rank < 100) return rank;

    // 达到100级后，每额外增加500万分再提升1级
    const level100Points = a * Math.pow(99, b);
    const extraLevels = Math.floor(Math.max(points - level100Points, 0) / 5_000_000);
    return 100 + extraLevels;
}

export function getAvatarFrame(points) {
    let rank = getRank(points);
    if (rank >= 100) {
        return new URL(`../assets/xinJi/images/avatarFrame/7.png`, import.meta.url).href
    }
    if (rank >= 0 && rank <= 99 && rank % 16 === 0) {
        return new URL(`../assets/xinJi/images/avatarFrame/${rank / 16 + 1}.png`, import.meta.url).href
    } 
    else {
        return new URL(`../assets/xinJi/images/avatarFrame/${Math.floor(rank / 16) + 1}.png`, import.meta.url).href
    }
}

export function getBadge(points) {
    let rank = getRank(points);
    if (rank >= 100) {
        return new URL(`../assets/xinJi/images/badge/25.png`, import.meta.url).href
    }
    if (rank >= 0 && rank <= 99 && rank % 4 === 0) {
        return new URL(`../assets/xinJi/images/badge/${rank / 4 + 1}.png`, import.meta.url).href
    } else {
        return new URL(`../assets/xinJi/images/badge/${Math.floor(rank / 4) + 1}.png`, import.meta.url).href
    }
}

export function getNumBox(points) {
    let rank = getRank(points);
    let index = 0;

    if (rank >= 100) {
        index = 7;
    } else if (rank >= 0 && rank <= 99 && rank % 16 === 0) {
        index = rank / 16 + 1;
    } else {
        index = Math.floor(rank / 16) + 1;
    }
    
    // 使用toString().length获取数字位数
    const rankDigits = rank.toString().length > 3 ? 3 : rank.toString().length;
    
    return new URL(`../assets/xinJi/images/avatarFrame/${index}-${rankDigits}.png`, import.meta.url).href
}

export function getBadgeStyle(points) {
    let rank = getRank(points);

    if (rank >= 1 && rank <= 15) {
        return 'linear-gradient( 0deg, rgb(120,72,43) 0%, rgb(181,151,124) 100%)';
    } else if (rank >= 16 && rank <= 31) {
        return 'linear-gradient( 0deg, rgb(65,65,65) 0%, rgb(187,188,188) 100%)';
    } else if (rank >= 32 && rank <= 47) {
        return 'linear-gradient( 0deg, rgb(157,94,1) 0%, rgb(225,206,107) 100%)';
    } else if (rank >= 48 && rank <= 63) {
        return 'linear-gradient( 0deg, rgb(65,65,65) 0%, rgb(187,188,188) 100%)';
    } else if (rank >= 64 && rank <= 79) {
        return 'linear-gradient( 0deg, rgb(57,98,136) 0%, rgb(129,170,211) 100%)';
    } else if (rank >= 80 && rank <= 95) {
        return 'linear-gradient( 0deg, rgb(2,5,9) 0%, rgb(12,143,247) 100%)';
    } else if (rank >= 96) {
        return 'linear-gradient( 0deg, rgb(83,34,0) 0%, rgb(228,126,17) 100%)';
    }
}

export function getLevel(points) {
    let level = getRank(points);

    if (level >= 1 && level <= 16) {
        return '倔强青铜';
    } else if (level >= 17 && level <= 32) {
        return '秩序白银';
    } else if (level >= 33 && level <= 48) {
        return '荣耀黄金';
    } else if (level >= 49 && level <= 64) {
        return '尊贵铂金';
    } else if (level >= 65 && level <= 80) {
        return '永恒钻石';
    } else if (level >= 81 && level <= 96) {
        return '超凡大师';
    } else if (level >= 97) {
        return '最强王者';
    }
}

// 重新加载图片
export function handleAvatarError (event, originalSrc) {
    const img = event.target
    if (img.dataset.reloaded === 'true') {
        return
    }
    img.dataset.reloaded = 'true';
    // 添加时间戳以防止缓存
    img.src = originalSrc + (originalSrc.includes('?') ? '&' : '?') + `v=${new Date().getTime()}`;
};
